package org.lwd.datalink.domain.media;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.Alias;
import org.lwd.datalink.domain.BaseEntity;
import org.lwd.datalink.domain.Storable;
import org.lwd.datalink.domain.task.TaskInfo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: weidong.lin
 * @Description: 介质同步映射类，定义同步行为.
 * @Date: create in 2024/4/2 14:18
 */
@Getter
@Setter
@Alias("mediaMapping")
public class MediaMappingInfo extends BaseEntity implements Serializable, Storable {
    private Long id;
    private Long taskId;
    private Long sourceMediaId;
    private Long targetMediaSourceId;
    private String targetMediaName;
    private String targetMediaNamespace;
    private String parameter = "{}";
    private ColumnMappingMode columnMappingMode;
    private Long writePriority;
    private boolean valid;
    private Long interceptorId;
    private String joinColumn;
    private boolean esUsePrefix;
    private String esRouting;
    private String esRoutingIgnore;
    private String prefixName;
    private String geoPositionConf;
    private String skipIds;
    //------------------------------------------------------------------------------------------------------------------
    //------------------------------getter&setter methods for database fields-------------------------------------------
    //------------------------------------------------------------------------------------------------------------------

    private Long bizLineId;

    public void setParameter(String parameter) {
        this.parameter = parameter;
        if (!org.apache.commons.lang.StringUtils.isBlank(parameter) && !Objects.equals(parameter, "{}")) {
            parameterObj = JSONObject.parseObject(parameter, new TypeReference<Object>() {}, Feature.SupportAutoType);
        } else {
            parameterObj = null;
        }
    }


    public void setGeoPositionConf(String geoPositionConf) {
        this.geoPositionConf = geoPositionConf;
        if (!StringUtils.isEmpty(geoPositionConf)) {
            this.geoPositionMappings = JSONObject.parseArray(geoPositionConf, GeoPositionMapping.class);
        } else {
            this.geoPositionMappings = null;
        }
    }

    //------------------------------------------------------------------------------------------------------------------
    //-----------------------------------------fields for business------------------------------------------------------
    //------------------------------------------------------------------------------------------------------------------


    private MediaInfo sourceMedia;
    private MediaSourceInfo targetMediaSource;
    private List<MediaColumnMappingInfo> columnMappings = new ArrayList<>(0);
    private TaskInfo taskInfo;
    private List<GeoPositionMapping> geoPositionMappings;
    private transient Object parameterObj;


    @Override
    public String toString() {
        return "MediaMappingInfo{" +
                "id=" + id +
                '}';
    }
}
